constructor is invoked only once when the contract is created.

Hence, we can keep any kind of initialization code within it, as shown

as follows:

// SPDX-License-Identifier: Some Identifier

pragma solidity ^0.8.10;

contract ConstructorSample {

uint a;

constructor() {

a = 7;

}

function someFunction() public view returns(uint){

return a;

}

}

In this example, you can find that the value of the variable is initiated

in the constructor.

If there is no constructor, the contract will assume the default

constructor, which is equivalent to a constructor with an empty body

constructor() {}.

Prior to this version, we had to specify the visibility of the

constructors as either internal or public, but now it’s no longer

required.

2.5.8.2 Setter() and Getter()

These are special types of functions where we can allocate and

retrieve the values in the variables.

In the Setter() functions, which we actually named as setName(),

setAge(uint age) etc., we set or allocate the values to the State

variables. In the Getters, which are usually named after what we are

retrieving such as getName(), getAge(), we retrieve the values of the

state variable. These functions are usually not used for the

implementation of any other business logic.

Refer to the following code: